<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="600px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="150px">
      <el-form-item label="穴位名称" prop="acupointName" required>
        <el-input
          v-model="form.acupointName"
          placeholder="请输入穴位名称"
          :maxlength="8"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="所属分组" prop="groupId" required>
        <el-select v-model="form.groupId" placeholder="请选择">
          <el-option
            v-for="(item, index) in groupList"
            :key="index"
            :label="item.groupName"
            :value="item.groupId"
          ></el-option>
        </el-select>
        <el-button
          class="ml-10"
          type="primary"
          size="mini"
          @click="handleOpen()"
          >新建分组</el-button
        >

        <EditGroup ref="EditGroup" @refresh="getGroupList" />
      </el-form-item>
      <el-form-item label="排序" prop="sort" required>
        <el-input-number
          v-model="form.sort"
          controls-position="right"
          placeholder="数字越小越排前"
          :step="1"
          step-strictly
          :min="1"
          :max="999"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="穴位简介" prop="acupointIntroduce">
        <el-input
          type="textarea"
          :rows="3"
          placeholder="请输入穴位简介"
          v-model="form.acupointIntroduce"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="穴位图" prop="acupointImg">
        <ImageUpload v-model="form.acupointImg" />
      </el-form-item>
      <el-form-item label="穴位介绍视频" prop="acupointVideo">
        <FileUpload
          v-model="form.acupointVideo"
          :limit="1"
          :fileSize="100"
          :fileType="['mp4']"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload'
import { getAcupointGroupList, addAcupoint, updateAcupoint } from '@/api/saas/tcm.js'
import EditGroup from './editGroup'
import { deepClone } from '@/utils';

export default {
  data() {
    return {
      title: "新建穴位",
      open: false,
      form: {
        id: undefined,
        acupointName: undefined,
        groupId: undefined, //组ID
        sort: undefined, //排序
        acupointIntroduce: undefined,
        acupointImg: undefined,
        acupointVideo: undefined
      },
      groupList: []
    };
  },
  components: {
    ImageUpload,
    EditGroup
  },
  methods: {
    openModal(row) {
      if(row && row.id){
        this.title = "编辑穴位";
        this.form = deepClone(row);
      }

      this.open = true;
      this.getGroupList();
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    getGroupList() {
      getAcupointGroupList({ pageNum: 1, pageSize: 9999 }).then(res => {
        this.groupList = res.rows;
      })
    },
    handleOpen() {
      this.$refs.EditGroup.openModal();
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateAcupoint(this.form).then(() => {
              this.$message.success("修改成功");
              this.$emit("refresh");
              this.closeModal();
            })
          } else {
            addAcupoint(this.form).then(() => {
              this.$message.success("新增成功");
              this.$emit("refresh");
              this.closeModal();
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
